from datetime import datetime

from flask import jsonify


def success_api(data=None, code=200, msg: str = "成功"):
    """统一响应格式"""
    return (
        jsonify(
            {
                "code": code,
                "msg": msg,
                "data": data,
                "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            }
        ),
        code,
    )


def fail_api(data=None, code=200, msg: str = "失败"):
    return (
        jsonify(
            {
                "code": code,
                "msg": msg,
                "data": data,
                "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            }
        ),
        code,
    )


def table_api(msg: str = "", count=0, data=None, limit=10):
    res = {"msg": msg, "code": 0, "data": data, "count": count, "limit": limit}
    return jsonify(res)
